package lschgcdev.net;

import java.io.IOException;
import java.net.InetSocketAddress;

import lschgcdev.apis.DataUser;
import lschgcdev.net.service.Service;

public class DataNet extends DataUser<Service>{
	
	public Thread listener;
	public InetSocketAddress inetSA;
	public boolean multiuser, tcp;
	
	public void attempt(final CSHost host){
		if(listener == null && tcp && multiuser){
			listener = new Thread(){
				@Override
				public void run() {
					while(!isInterrupted()){
						host.accept();
					}
				}
			};
		}
		
		if(listener != null)
			listener.start();
	}
	
	public void service(final CSUser user){
		if(listener == null && tcp){
			listener = new Thread(){
				@Override
				public void run() {
					while(!isInterrupted()){
						try {
							user.recive();
						} catch (IOException e) {
							try {
								user.close();
								this.interrupt();
							} catch (IOException e1) {
								e1.printStackTrace();
							}
						}
					}
				}
			};
		}
		
		if(listener != null)
			listener.start();
	}
	
	public Service service(String ID){
		return getData(ID);
	}
	
	
}
